package com.hylanda.processors.getdomainname.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetDomainNameUtil {

	public static String getDomainName(String url) {
		if (url.contains("?")) {
			url = url.substring(0,url.lastIndexOf("?"));
		}
		
		// 提取主域名
		if (url == null ||"".equals(url)) {
			return null;
		}
		String RE_TOP = "[0-9a-zA-Z]+((\\.com)|(\\.cn)|(\\.org)|(\\.net)|(\\.edu)|(\\.com.cn))";
		Pattern pattern = Pattern.compile(RE_TOP);
		Matcher matcher = pattern.matcher(url);
		List<String> strList = new ArrayList<>();
		while (matcher.find()) {
			strList.add(matcher.group());
		}
		String cateid = strList.toString();
		cateid = cateid.substring(1, cateid.length() - 1);
		return cateid;
	}
	public static void main(String[] args) {
		String domainName = getDomainName("http://www.myzaker.com/news/home_new.php?f=myzaker_com&url=http%3A%2F%2Fiphone.myzaker.com%2Fzaker%2Fblog2news.php%3Fapp_id%3D4%26since_date%3D1522106121%26nt%3D1%26next_aticle_id%3D5abadc3a9490cb1e3800001c%26_appid%3Diphone%26opage%3D2%26otimestamp%3D201%");
		System.out.println(domainName);
	}

}
